const app = getApp();
Page({
  data:{
    showModalStatus: false,
    showModalStatus1: false,
    showModalStatus2: false,
    contacts:[],
    searchItem:'',
    userInfo: {
      avatarUrl: "",//用户头像
      nickName: "",//用户昵称
    }
  },
  onShow:function(){
    const _this = this;
    wx.getStorage({
      key: 'contact',
      success: function (res) {
        // console.log(res.data)
        // console.log('----------------------------')
        _this.setData({
          searchItem:res.data
        })
      }
    })
    this.getContacts();
    this.getCurrentInfo();
    // console.log(_this.data.searchItem)
    if (!_this.data.searchItem){
      _this.setData({
        searchItem: ''
      })
    }
  },
  getCurrentInfo(){
    const _this = this;
    // 用户信息
    let baseInfo = app.globalData.baseInfo;
    _this.setData({
      currentId: baseInfo.id,
      name: baseInfo.name
    })
  },
  //其他用户信息
  toNext:function(e){
    console.log(e.detail.value.bankId);
    const _this = this;
    if (e.detail.value.bankId){
      _this.setData({
        bankId: e.detail.value.bankId
      })
    }else if(_this.searchItem){
      _this.setData({
        bankId: _this.data.searchItem
      })
    }
    console.log(_this.data.bankId)
    if (_this.data.bankId){
      wx.request({
        url: app.globalData.path + 'services/app/applets/GetOtherUserInfo',
        data: {
          phoneNumOrIdentityCard: _this.data.bankId
        },
        method: 'POST',
        header: {
          'content-type': 'application/json', // 默认值
          'Authorization': 'Bearer ' + app.globalData.authorization
        },
        success: function (res) {
          console.log(res.data);
          if (res.data.success) {
            if (res.data.result.length == 1) {
              if (res.data.result[0].id == _this.data.currentId) {
                wx.showModal({
                  content: '不能给自己转账！',
                  showCancel: false,
                  success: function (res) {
                    if (res.confirm) {
                      console.log('用户点击确定')
                    }
                  }
                });
              } else {
                _this.tojump(e.detail.value.bankId);
              }
            } else if (res.data.result.length > 1) {
              _this.setData({
                showModalStatus1: true,
                contacts2: res.data.result
              })
            } else if (res.data.result.length == 0) {
              _this.setData({
                showModalStatus2: true
              })
            }
          } else {
            wx.showToast({
              title: res.data.error.message,
              icon: 'none',
              duration: 2000
            })
          }
        }
      })
    }else{
      wx.showToast({
        title: '手机号和姓名不能为空',
        icon: 'none',
        duration: 2000
      })
    }
    
  },
  // 打开弹框
  openmodal: function () {
    this.setData({
      showModalStatus2: false
    })
    this.setData({
      showModalStatus: true
    })
  },
  // 关闭弹框
  closemodal: function () {
    this.setData({
      showModalStatus: false
    })
  },
  // 关闭弹框
  closemodal1: function () {
    this.setData({
      showModalStatus1: false
    })
  },
  // 关闭弹框
  closemodal2: function () {
    this.setData({
      showModalStatus2: false
    })
  },
  // 跳转
  tojump:function(id){
    wx.removeStorage({
      key: 'contact',
      success: function (res) {
      }
    })
    wx.navigateTo({
      url: '../othernext/othernext?bankId=' + id + '&from=transferother'
    })
  },
  // 分享
  onShareAppMessage: function (res) {
    let title = '';
    if (this.data.name){
      title = this.data.name+'邀请你使用锦和薪金宝收取佣金！';
    }else{
      title = '你的好友邀请你使用锦和薪金宝收取佣金！'
    }
    var shareObj = {
      title:title,
      path: 'pages/login/login',
      imageUrl:'../../static/icon_share.png',
      success:function(res){
        if (res.errMsg == 'shareAppMessage:ok') {}
      },
      fail:function(res){
        if (res.errMsg == 'shareAppMessage:fail cancel') {}
        else if (res.errMsg == 'shareAppMessage:fail') {}
      }
    }
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return shareObj;
  },
  aa:function(){
    this.setData({
      showModalStatus2: true
    })
  },
  // 最近转账
  getContacts(){
    const _this = this;
    wx.request({
      url: app.globalData.path + 'services/app/applets/GetRecentContacts',
      data: {
        type : 1
      },
      method: 'POST',
      header: {
        'content-type': 'application/json', // 默认值
        'Authorization': 'Bearer ' + app.globalData.authorization
      },
      success: function (res) {
        console.log(res.data);
        if (res.data.success) {
          _this.setData({
            contacts:res.data.result.items
          })
        } else {
          wx.showToast({
            title: res.data.error.message,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  // 点击事件
  getContact(e){
    const _this = this;
    var idx = e.currentTarget.id;
    _this.setData({
      searchItem: _this.data.contacts[idx].phoneNumber
    })
  },
  // 点击事件
  getContact2(e) {
    const _this = this;
    _this.setData({
      showModalStatus1: false
    })
    var idx = e.currentTarget.id;
    if (_this.data.currentId != _this.data.contacts2[idx].id){
      wx.navigateTo({
        url: '../othernext/othernext?bankId=' + _this.data.contacts2[idx].userName + '&from=transferother'
      })
    }else{
      wx.showModal({
        content: '不能给自己转账！',
        showCancel: false,
        success: function (res) {
          if (res.confirm) {
            console.log('用户点击确定')
          }
        }
      });
    }
    
    // _this.setData({
    //   searchItem: _this.data.contacts2[idx].userName
    // })
  },
  // 联系人
  toContact(){
    wx.navigateTo({
      url: '../contacts/contacts',
    })
  }
})